package com.iflytek.topN;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class BestBean implements WritableComparable<BestBean> {

    private String type;//类型
    private String name;//名字
    private long comments;//评论数

    public BestBean() {
    }

    public BestBean(String type, String name, long comments) {
        this.type = type;
        this.name = name;
        this.comments = comments;
    }

    public void set (String type, String name, long comments) {
        this.type = type;
        this.name = name;
        this.comments = comments;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getComments() {
        return comments;
    }

    public void setComments(long comments) {
        this.comments = comments;
    }

    @Override
    public String toString() {
        return "BestBean{" +
                "type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", comments=" + comments +
                '}';
    }

    //todo 排序规则 根据州type正序进行排序 如果类型相同 则根据评论数comments倒序排序
    @Override
    public int compareTo(BestBean o) {

        int result ;

        int i = type.compareTo(o.getType());

        if ( i > 0) {
            result =1;
        } else if (i <0 ) {
            result = -1;
        } else {
            // 评论数倒序排序
            result = comments > o.getComments() ? -1 : 1;
        }

        return result;

    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(type);
        out.writeUTF(name);
        out.writeLong(comments);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.type =in.readUTF();
        this.name =in.readUTF();
        this.comments =in.readLong();
    }
}